﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.Text;
using System.Xml;
using System.IO;

namespace Content_Extension
{

    [ContentProcessor(DisplayName = "Tile Level Processor")]
    public class TileMapProcessor : ContentProcessor<XmlDocument, TileMapContent>
    {
        public override TileMapContent Process(XmlDocument input, ContentProcessorContext context)
        {
            TileMapContent tileMap = new TileMapContent();

            foreach (XmlNode rootNode in input.DocumentElement.ChildNodes)
            {
                if (rootNode.Name == "tileset")
                {
                    TileSetTextureContent textureContent = new TileSetTextureContent();

                    textureContent.tileWidth = int.Parse(rootNode.Attributes["tilewidth"].Value);
                    textureContent.tileHeight = int.Parse(rootNode.Attributes["tileheight"].Value);
                    string file = ""; 


                    foreach (XmlNode child in rootNode)
                        if (child.Name == "image")
                            file = child.Attributes["source"].Value;

                    file = file.Replace("../", "../Content/");

                    string Name = rootNode.Attributes["name"].Value;

                    OpaqueDataDictionary data = new OpaqueDataDictionary();
                    data.Add("GenerateMipmaps", true);

                    textureContent.Texture = context.BuildAsset<TextureContent, TextureContent>(new ExternalReference<TextureContent>(file),
                        "TextureProcessor",
                        data,
                        "TextureImporter",
                        Name);
                        
                        
                        //Path.GetDirectoryName(file) + "/" + Path.GetFileNameWithoutExtension(file));

                    tileMap.Textures.Add(textureContent);
                }
                else if (rootNode.Name == "layer")
                {
                    TileLayerContent layer = new TileLayerContent();
                    layer.name = rootNode.Attributes["name"].Value;
                    layer.width = int.Parse(rootNode.Attributes["width"].Value);
                    layer.height = int.Parse(rootNode.Attributes["height"].Value);

                    foreach (XmlNode layerNode in rootNode)
                    {
                        if (layerNode.Name == "data")
                        {
                            layer.Layout = new int[layer.height, layer.width];

                            string layout = layerNode.InnerText;

                            string[] lines = layout.Split('\r', '\n');

                            int row = 0;

                            foreach (string line in lines)
                            {
                                string realLine = line.Trim();

                                if (string.IsNullOrEmpty(realLine))
                                    continue;

                                string[] cells = realLine.Split(',');

                                for (int x = 0; x < layer.width; x++)
                                {
                                    int cellIndex = int.Parse(cells[x]);

                                    layer.Layout[row, x] = cellIndex;
                                }

                                row++;
                            }
                        }
                    }
                    tileMap.Layers.Add(layer);
                }

                else if (rootNode.Name == "objectgroup")
                {
                    foreach (XmlNode objNode in rootNode)
                    {
                        if (objNode.Name == "object")
                        {
                            ObjectLayerContent objItem = new ObjectLayerContent();

                            if (objNode.Attributes["gid"] != null)
                                objItem.gridId = int.Parse(objNode.Attributes["gid"].Value);

                            objItem.x = int.Parse(objNode.Attributes["x"].Value);
                            objItem.y = int.Parse(objNode.Attributes["y"].Value);

                            if(objNode.Attributes["width"] != null)
                                objItem.width = int.Parse(objNode.Attributes["width"].Value);

                            if (objNode.Attributes["height"] != null)
                                objItem.height = int.Parse(objNode.Attributes["height"].Value);

                            tileMap.Objects.Add(objItem);
                        }
                    }
                }




            }

            return tileMap;
        }
    }

}
